package com.ebelter.temperature.activitys;


import android.graphics.Color;
import android.util.TypedValue;
import android.view.View;
import android.widget.AbsListView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.ebelter.btcomlib.bases.activity.BaseActivity;
import com.ebelter.btcomlib.common.Constants;
import com.ebelter.btcomlib.models.https.IHttpRequestField;
import com.ebelter.btcomlib.models.https.NetUtils;
import com.ebelter.btcomlib.models.https.interfaces.HttpResponse;
import com.ebelter.btcomlib.models.https.responses.GetTempertureData;
import com.ebelter.btcomlib.utils.StatusBarUtil;
import com.ebelter.btcomlib.utils.StringUtils;
import com.ebelter.btcomlib.utils.ToastUtil;
import com.ebelter.btcomlib.utils.UserSpUtil;
import com.ebelter.btcomlib.utils.ViewUtils;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.ebelter.btcomlib.views.dialogs.LoadingDialog;
import com.ebelter.temperature.R;
import com.ebelter.temperature.adapters.THostoryListAdapter;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

public class T_HistoryActivity extends BaseActivity {

    private static final String TAG="T_HistoryActivity";


    private LinearLayout root_ll;
    TextView topTitleTv;
    RelativeLayout no_history_ly;
    ListView history_lv;
    THostoryListAdapter adapter;


    private int startPage;
    private int pageSize=10;

    private long lastLoadingTime;

    View listFootView;
    TextView noMoreTv;

    @Override
    protected int loadLayoutById() {
        return R.layout.activity_t_history;
    }

    @Override
    protected void initView() {
        StatusBarUtil.setColor(this, Color.parseColor("#787ede"));
        FV();
        root_ll.setBackgroundColor(Color.parseColor("#787ede"));
        adapter=new THostoryListAdapter(this);
        history_lv.setAdapter(adapter);

        listFootView = View.inflate(this, R.layout.no_history, null);
        noMoreTv=listFootView.findViewById(R.id.no_data_tv);
        noMoreTv.setText(R.string.No_more);
        noMoreTv.setTextSize(TypedValue.COMPLEX_UNIT_SP,14);
        history_lv.addFooterView(listFootView);
        ViewUtils.hideView(listFootView);

        getHostoryListDatas(startPage,pageSize);

        history_lv.setOnScrollListener(new AbsListView.OnScrollListener(){
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState){
                // 当不滚动时
                if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
                    // 判断是否滚动到底部
                    if (view.getLastVisiblePosition() == view.getCount() - 1) {
                        //加载更多功能的代码
                        if (System.currentTimeMillis()-lastLoadingTime>3000){
                            LogUtils.i(TAG,"---------加载更多");
                            lastLoadingTime=System.currentTimeMillis();
                            getHostoryListDatas(++startPage,pageSize);
                        }
                    }
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            }
        });

    }

    private void FV() {
        topTitleTv = findViewById(R.id.top_title_tv);
        no_history_ly = findViewById(R.id.no_history_ly);
        history_lv = findViewById(R.id.history_lv);
        root_ll = findViewById(R.id.root_ll);


    }
    private LoadingDialog loadingDialog;
    public void showLoadingDialog(String tip){
        if (loadingDialog==null){
            loadingDialog=new LoadingDialog(this);
        }
        if (!loadingDialog.isShowing()){
            loadingDialog.show();
        }
        loadingDialog.setTip(tip);

    }

    public void dissLoadingDialog(){
        if (loadingDialog!=null&&loadingDialog.isShowing()){
            loadingDialog.cancel();
        }
    }


    public void getHostoryListDatas(final int startPage,final int pageSize) {
        if (!NetUtils.available()){
            ToastUtil.show(R.string.Network_error);
            return;
        }
        showLoadingDialog(StringUtils.getResStr(R.string.loading));

        Map<String,Serializable> paramMap=new HashMap<>();
        paramMap.put(IHttpRequestField.USER_ID, UserSpUtil.readLong(Constants.IUser.USER_ID));
        paramMap.put(IHttpRequestField.START_INDEX, startPage);
        paramMap.put(IHttpRequestField.PAGE_SIZE, pageSize);

        NetUtils.getInstance().getTempertureData1(this, paramMap, new HttpResponse<GetTempertureData>() {
            @Override
            public void result(boolean isRequestSuccess, String requestDes, GetTempertureData getTempertureData, String s) {
                LogUtils.i(TAG,"-------s = "+s);
                dissLoadingDialog();

                LogUtils.i(TAG,"---------pageSize = "+pageSize+"---startPage = "+startPage);
                LogUtils.i(TAG,"---------s = "+s);

                if (!isRequestSuccess){
                    ToastUtil.show(R.string.Network_connection_error);
                }else {
                    if (getTempertureData.getResultCode()==0
                            &&getTempertureData.getResultData()!=null
                            &&getTempertureData.getResultData().getData()!=null){

                        if (adapter!=null) adapter.addData(getTempertureData.getResultData().getData());

                        if (getTempertureData.getResultData().getData().size()==0){
                            ViewUtils.displayView(listFootView);
                        }

                    }
                }
                if (adapter!=null){
                    if (adapter.getCount()==0){
                        ViewUtils.displayView(no_history_ly);
                    }else {
                        ViewUtils.hideView(no_history_ly);
                    }
                }


            }
        });

    }

    @Override
    protected void initData() {
        topTitleTv.setText("温度列表");
    }

    //返回键被按下
    public void backClick(View view){
        LogUtils.i(TAG,"返回键被按下");
        finish();
    }


}
